/*
 * 
 * GNU GPL v2
 */

package com.ist412.pocke;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

/**
 * This is the main activity which only appears to gather necessary data and display the disclaimer screen.
 * <p>
 * After the disclaimer screen, the user is brought to CalendarActivity where they will spend the remainder
 * of their time in the application.
 * <p>
 * If the user manages to come back to MainActivity, the application is immediately terminated.
 * 
 * @author Rob C & JB
 * @version 1.0
 */
public class MainActivity extends Activity implements OnClickListener {
	
	private static Activity				_main;
	private static ConfigExtra			_extra;
	public static ArrayList<Workout> 	_workouts;
	public static ArrayList<Exercise>	_exercises;
	public static final int 			_BG_BLUE 			= 0xF0050A0;
	public static final String			_FILENAME_WORKOUTS	= "workoutdata.pocke";
	public static final String			_FILENAME_EXERCISES	= "exercisedata.pocke";
	public static final String			_FILENAME_EXTRA	= "extradata.pocke"; // int values containing extras such as time and vibratetime
	public static int					_restTimer, _vibrateTime;
	private int							_egg;
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        // Remove title & Make fullscreen
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
        WindowManager.LayoutParams.FLAG_FULLSCREEN);
        
        setContentView(R.layout.disclaimer);
        
        _main 			= this;
        _restTimer		= 60000; // 1 minute
        _vibrateTime	= 200;
		_egg 			= 0;
        
        if (!loadData()) {
        	_workouts = new ArrayList<Workout>();
        	_exercises = new ArrayList<Exercise>();
        	_extra = new ConfigExtra(60000, 0);
        } else {
        	_restTimer = _extra.getRestTime();
        	_vibrateTime = _extra.getVibrateTime();
        }
        
        // Set up acknowledgment menu
        Button btnUnderstand = (Button)findViewById(R.id.disclaimer_btnUnderstand);
        ImageView imgLogo 	= (ImageView) findViewById(R.id.disclaimer_imgLogo);
        ImageView imgAbout 	= (ImageView) findViewById(R.id.disclaimer_imgAbout);
        
        btnUnderstand.setOnClickListener(this);
        imgLogo.setOnClickListener(this);
        imgAbout.setOnClickListener(this);
    } // end onCreate()
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    
    @Override
    public void onClick(View v) {
    	Intent intent;
    	
    	switch(v.getId()) {
    		case R.id.disclaimer_btnUnderstand:
				// Start Calendar
				intent = new Intent(v.getContext(), CalendarActivity.class);
				startActivity(intent);
				finish();
    			break;
    		case R.id.disclaimer_imgLogo:
    			_egg ++;
    			if (_egg > 2) {
    				_egg = 0;
    				showToast("Training made easy!");
    			}
    			break;
    		case R.id.disclaimer_imgAbout:
    			intent = new Intent(v.getContext(), AboutActivity.class);
    			startActivity(intent);
    			break;
    	} // end switch (v)
    	
    } // end override onClick()
    
    /**
     * @return true - successful load operation
     */
    public static final boolean loadData() {
    	
    	FileInputStream fisWorkouts = null, fisExercises = null, fisExtra = null;;
    	ObjectInputStream oisWorkouts = null, oisExercises = null, oisExtra = null;;
    	
    	try {
    		fisWorkouts = _main.openFileInput(_FILENAME_WORKOUTS);
    		fisExercises = _main.openFileInput(_FILENAME_EXERCISES);
    		fisExtra 	= _main.openFileInput(_FILENAME_EXTRA);
	    	oisWorkouts = new ObjectInputStream(fisWorkouts);
	    	oisExercises = new ObjectInputStream(fisExercises);
	    	oisExtra = new ObjectInputStream(fisExtra);
	    	
	    	_workouts = (ArrayList<Workout>) oisWorkouts.readObject();
	    	_exercises = (ArrayList<Exercise>) oisExercises.readObject();
	    	_extra	= (ConfigExtra) oisExtra.readObject();
	    	
	    	oisWorkouts.close();
	    	oisExercises.close();
	    	oisExtra.close();
	    	fisWorkouts.close();
	    	fisExercises.close();
	    	fisExtra.close();
    	} catch (NullPointerException npe) {
    		// TODO Handle this error better
    		Log.e("Main", "Load - npe");
    		npe.printStackTrace();
    		return false;
    	} catch (FileNotFoundException fnfe) {
    		Log.e("Main", "Load - fnfe");
    		fnfe.printStackTrace();
    		return false;
    	} catch (IOException ioe) {
    		Log.e("Main", "Load - ioe");
    		Toast.makeText(_main, "IOE - Failed to load workout data", Toast.LENGTH_SHORT).show();
    		ioe.printStackTrace();
    		return false;
    	} catch (ClassNotFoundException cnfe) {
    		Log.e("Main", "Load - cnfe");
    		Toast.makeText(_main, "Failed to load workout data", Toast.LENGTH_SHORT).show();
			cnfe.printStackTrace();
			try { oisWorkouts.close(); } catch (IOException ioe) { }
			return false;
		}
    	
    	return true;
    } // end loadData()
    
    /**
     * @return true - successful save operation
     */
    public static final boolean saveData() {
    	
    	_extra.setRestTime(_restTimer);
    	_extra.setVibrateTime(_vibrateTime);
    	
    	FileOutputStream fisWorkout = null, fisExercise = null, fisExtra = null;
    	ObjectOutputStream oosWorkout = null, oosExercise = null, oosExtra = null;
    	
    	try {
			fisWorkout = _main.openFileOutput(_FILENAME_WORKOUTS, Context.MODE_PRIVATE);
			fisExercise = _main.openFileOutput(_FILENAME_EXERCISES, Context.MODE_PRIVATE);
			fisExtra = _main.openFileOutput(_FILENAME_EXTRA, Context.MODE_PRIVATE);
			
			oosWorkout = new ObjectOutputStream(fisWorkout);
			oosExercise = new ObjectOutputStream(fisExercise);
			oosExtra = new ObjectOutputStream(fisExtra);
			
			oosWorkout.writeObject(_workouts);
			oosExercise.writeObject(_exercises);
			oosExtra.writeObject(_extra);
			
			oosWorkout.close();
			oosExercise.close();
			oosExtra.close();
			fisWorkout.close();
			fisExercise.close();
			fisExtra.close();
		} catch (FileNotFoundException fnfe) {
			Log.e("Main", "Save - fnfe");
			Toast.makeText(_main, "Failed to save data", Toast.LENGTH_SHORT).show();
			fnfe.printStackTrace();
			return false;
		} catch (IOException ioe) {
			Log.e("Main", "Save - ioe");
			Toast.makeText(_main, "Failed to save data", Toast.LENGTH_SHORT).show();
			ioe.printStackTrace();
			return false;
		}
    	
    	return true;
    } // end saveData()
    
    private final void showToast(String text) {
    	Toast.makeText(this, text, Toast.LENGTH_SHORT).show();
    }
    
} // end class MainActivity























